/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fsd.semana1.aula.echo;

import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 *
 * @author nfsa
 */
public class EchoThread extends Thread {

    private SocketChannel socket;
    
    public EchoThread (SocketChannel _socket)
    {
        socket = _socket;
    }

    @Override
    public void run()
    {
        try
        {
            ByteBuffer bbuf = ByteBuffer.allocate(1024);
            boolean proceed = true;
            while (proceed)
            {
                int received = socket.read(bbuf);
                if (received <= 0)
                {
                    proceed = false;
                }
                else
                {
                    bbuf.flip();
                    socket.write(bbuf);
                    bbuf.clear();
                }
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}
